﻿// ....................................................................................................................
//	Author: Copyright (C) 2012 Ing. Marek Hubal
//	Email: hubal@acasoftware.org
// ....................................................................................................................

using System;

namespace core
{
    /// <summary>Trieda ITextBox</summary>
	public class ITextBox : System.Windows.Forms.TextBox
	{

        /// <summary>Povolenie autocomplete funkcionality</summary>
        /// <param name="pData">data</param>
        public void EnableAutocomplete(string pData)
        {
            try
            {
                string[] Values = pData.Split(new char[] { (char)0x1 }, StringSplitOptions.RemoveEmptyEntries); // Rozbijem na retazce
                AutoCompleteCustomSource.AddRange(Values); // Priradim
                AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource; // Nastavim 
                AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
            }
            catch
            {
                AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.None;
            }
        }
        /// <summary>Aktualizacia autocomplete dat pridanim aktualnej hodnoty a vratenie serializovaneho obsahu</summary>
        /// <returns>serializovany obsah</returns>
        public string UpdateAutoComplete()
        {
            System.Collections.Generic.List<string> L = new System.Collections.Generic.List<string>();

            bool pExists = false;
            string CurrValue = Text.Trim();

            foreach (string S in AutoCompleteCustomSource)
            {
                if (string.Compare(S, CurrValue) == 0) pExists = true; // Je uz aktualna hodnota v zozname?
                L.Add(S);
            }
            if ((!pExists) && (!string.IsNullOrEmpty(CurrValue))) L.Add(CurrValue); // Ak tam este nie je zaradim aj aktualny obsah
            return ICore.StringList2String(L, "\x1");
        }

        /// <summary>Test, ci je obsah platnym int cislom v danom rozsahu</summary>
        /// <param name="pMin">minimum</param>
        /// <param name="pMax">maximum</param>
        /// <returns>true / false</returns>
        public bool IsValidInt(int pMin = int.MinValue, int pMax = int.MaxValue)
        {
            int Res;
            if (!int.TryParse(Text.Trim(), out Res)) return false; // Pokusim sa previest na int
            return ((Res >= pMin) && (Res <= pMax)); // Zistim ci je v rozsahu
        }
        /// <summary>Test, ci je obsah platnym long cislom v danom rozsahu</summary>
        /// <param name="pMin">minimum</param>
        /// <param name="pMax">maximum</param>
        /// <returns>true / false</returns>
        public bool IsValidLong(long pMin = long.MinValue, long pMax = long.MaxValue)
        {
            long Res;
            if (!long.TryParse(Text.Trim(), out Res)) return false; // Pokusim sa previest na int
            return ((Res >= pMin) && (Res <= pMax)); // Zistim ci je v rozsahu
        }
    }
}
